package main

import (
	"fmt"
	"log"
	"net/http"
	"regexp"
)

func VerfyEmail(email string) bool {
	reg := regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`)
	return reg.MatchString(email)
}

func GetQQDataByWord(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm() //解析输入
	if err != nil {
		log.Fatal(err)
	}

	var word string
	var email string
	for k, v := range r.Form {
		if k == "word" {
			word = v[0]
		}
		if k == "email" {
			email = v[0]
		}
	}
	fmt.Println(word, email)
	if word == "" || email == "" {
		fmt.Fprintf(w, "用户输入有误")
	} else {
		//验证邮箱
		//数据发送给后端
		if !VerfyEmail(email) {
			fmt.Fprintf(w, "用户输入邮箱有误")
		} else {
			fmt.Fprintf(w, "尊贵的用户%s,业务已经开启，你的词语[%s]已经开始处理,稍后查看邮件", email, word)
			//"计算机,软件工程#yincheng01@163.com"
			//"机械,土木#yincheng0571@163.com"
			mysend := NewSendRabbitMQ("amqp://admin:admin@192.168.56.103:5672/", "FindQunByWord_task_queue")
			mysend.SendData(word + "#" + email)
			mysend.Close()
		}
	}
}

func main() {
	http.HandleFunc("/qq/GetQQDataByWord", GetQQDataByWord)
	http.ListenAndServe(":9000", nil)

}
